package gui.dialog;

import game.player.HumanNetwork;
import game.player.Network;
import gui.GUIController;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import techLayer.Database;
import techLayer.HighscoreFrame;
import techLayer.NetworkController;

public class SetUpNetworkGameDialog extends JDialog {

	private final int BLACK = 1, RED = 2;
	private GUIController controller;
	private JPanel panel = new JPanel();
	private JTextField jtxfName;
    private String IP = "";
    private JDialog parent = this;
    
	public SetUpNetworkGameDialog(GUIController controller) {
		this.controller = controller;

		setTitle("Network Menu");
		setSize(285, 350);
		setResizable(false);
		
		setModal(true);

		setLayout(new BorderLayout(50, 10));
		setLocationRelativeTo(null);
		
		setUpNetworkMenu();
		setVisible(true);

	}

	private void setUpNetworkMenu() {
		panel.setLayout(new GridLayout(8, 1, 0, 25));
		
		JButton jbnHost = new JButton("Host Game");
		JButton jbnJoin = new JButton("Join Game");
		jtxfName = new JTextField("Your name:");
		JButton jbnHighscore = new JButton("Highscore");
		JButton jbnBack = new JButton("Back");

		jbnHost.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				NetworkController network = new NetworkController();
				network.createServer(1337);
				String name = getPlayerName();
				
				String opName = network.recieveName();
				network.sendName(name);
				
				controller.getGameManager().addPlayers(
						  new HumanNetwork(name, 1, BLACK, network, controller.getGameManager()),
						  new Network(opName, 2, RED, network));
				initializeGamePlay();
			}
		});
		jbnJoin.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
                JoinGameDialog jgd = new JoinGameDialog(parent);         
			}
		});
        jbnHighscore.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                Database db = new Database();
                Iterator it = db.getHighscore();
                HighscoreFrame frame = new HighscoreFrame(it);
                db.close();
            }
        });
		jbnBack.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		panel.add(new JLabel("<html><h1>Network Game</h1></html>"));
		panel.add(jbnHost);
		panel.add(jbnJoin);
		panel.add(jtxfName);
		panel.add(jbnHighscore);
		panel.add(jbnBack);
		panel.add(new JLabel(""));

		add(new JLabel(""), BorderLayout.WEST);
		add(panel, BorderLayout.CENTER);
		add(new JLabel(""), BorderLayout.EAST);
	}
    private void connectToHost(String host){
        NetworkController network = new NetworkController();
        network.connect(host, 1337);
        String name = getPlayerName();

        network.sendName(name);
        String opName = network.recieveName();

        controller.getGameManager().addPlayers(
                  new Network(opName, 2, BLACK, network),
                  new HumanNetwork(name, 1, RED, network, controller.getGameManager()));
        initializeGamePlay();
    }

	private void initializeGamePlay() {
		controller.resetGameBoard();
		controller.startGame();
		setVisible(false);
	}

	class JoinGameDialog extends JDialog {
        private JTextField jtxIP = new JTextField("localhost");
        private JDialog dialog = this;
        
        public JoinGameDialog(JDialog dialog){
            super(dialog);
            setTitle("Join Game");
            setLocationRelativeTo(null);
            
            setVisible(true);
            
            setSize(200,100);
            setLayout(new GridLayout(2,2));
            
            initJoinGameDialog();
        }
        private void initJoinGameDialog(){
            JButton jbtOk = new JButton("Ok");
            JButton jbtCancel = new JButton("Cancel");
            
            jbtOk.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent e) {
                    IP = jtxIP.getText();
                    dialog.setVisible(false);
                    connectToHost(IP);
                }
            });
            jbtOk.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent e) {
                    dialog.setVisible(false);
                }
            });
            
            add(new JLabel("IP Adress:"));
            add(jtxIP);
            
            add(jbtOk);
            add(jbtCancel);
        }
	}
	private String getPlayerName() {
		return jtxfName.getText();
	}
}
